package com.hhh.petadopt.controller;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.hhh.petadopt.pojo.News;
import com.hhh.petadopt.pojo.NewsVo;
import com.hhh.petadopt.service.AdminNewsService;
import com.hhh.petadopt.utils.JsonUtils;
import com.hhh.petadopt.utils.Page;

/**
 * 新闻管理
 * @author 杜镇声声
 *
 */
@Controller
public class AdminNewscontroller {
	
	@Autowired
	private AdminNewsService adminnewsservice;
	
	
	@RequestMapping(value="/adminNews/Newslist.html")
	public String slectpages(NewsVo Vo,Model model) {
		//执行分页
		Page<News> page = adminnewsservice.selectpage(Vo);
		
		//将数据存放到域中
		model.addAttribute("page", page);
		model.addAttribute("total", page.getTotal());
		
		//信息回显
		model.addAttribute("ntitle", Vo.getNtitle());
		model.addAttribute("nwriter", Vo.getNwriter());
		model.addAttribute("nishot", Vo.getNishot());
		model.addAttribute("nisshow", Vo.getNisshow());
		
		return "admin/news";
	}
	
	//添加文件上传
	@RequestMapping(value="/adminNews/uploadadpic.html")
	@ResponseBody
	public String uploadPetPic1(MultipartFile imgss1) throws IllegalStateException, IOException{
	//设置图片名称
	String pname = UUID.randomUUID().toString().replace("-", "");
	//获取原本图片名称
	String originalFilename = imgss1.getOriginalFilename();
	//获取图片扩展名
	String extName = FilenameUtils.getExtension(originalFilename);
						
	//将文件存到upload文件夹下
	imgss1.transferTo(new File("D:/upload/news/"+pname+"."+extName));
				
	//将图片的名称进行拼接
	String pic = "/newspic/"+pname+"."+extName;
				
	String objectToJson = JsonUtils.objectToJson(pic);
	//将路径转换为json格式的字符串
	//返回拼接好的完整的图片路径
	return objectToJson;
	}
	
	
	/*@RequestMapping(value="/AdminNews/uploadvvv")
	@ResponseBody
	public String uploadPetPicccc(MultipartFile imgFile) throws IllegalStateException, IOException{
	//设置图片名称
	String pname = UUID.randomUUID().toString().replace("-", "");
	//获取原本图片名称
	String originalFilename = imgFile.getOriginalFilename();
	//获取图片扩展名
	String extName = FilenameUtils.getExtension(originalFilename);
						
	//将文件存到upload文件夹下
	imgFile.transferTo(new File("D:/upload/news/"+pname+"."+extName));
				
	//将图片的名称进行拼接
	String pic = "/newspic/"+pname+"."+extName;
				
	String objectToJson = JsonUtils.objectToJson(pic);
	//将路径转换为json格式的字符串
	//返回拼接好的完整的图片路径
	return objectToJson;
	}*/
	
	
	//添加
	@RequestMapping(value="/adminNews/addNews.html")
	@ResponseBody
	public String addNewss(News n) {
		//执行操作
		adminnewsservice.addnewsss(n);
		return "ok";
	}
	//编辑页面信息回显
	@RequestMapping(value="/adminNews/editNews.html")
	@ResponseBody
	public String editNewss(Integer id) {
		
		//执行查询
		News news = adminnewsservice.selectNewsById(id);
		//将数据转换为json格式
		String objectToJson = JsonUtils.objectToJson(news);
		return objectToJson;
	}
	
	//编辑页面图片上传
	//文件上传
	@RequestMapping(value="/adminNews/uploadEditpic.html")
	@ResponseBody
	public String uploadPetPic(MultipartFile imgsss) throws IllegalStateException, IOException{
		//设置图片名称
		String pname = UUID.randomUUID().toString().replace("-", "");
		//获取原本图片名称
		String originalFilename = imgsss.getOriginalFilename();
		//获取图片扩展名
		String extName = FilenameUtils.getExtension(originalFilename);
				
		//将文件存到upload文件夹下
		imgsss.transferTo(new File("D:/upload/news/"+pname+"."+extName));
		
		//将图片的名称进行拼接
		String pic = "/newspic/"+pname+"."+extName;
				
		String objectToJson = JsonUtils.objectToJson(pic);
		
		//返回拼接好的完整的图片路径
		return objectToJson;
	}
	
	//修改信息
	@RequestMapping(value="/adminNews/updateNewss.html")
	@ResponseBody
	public String editnews(News n) {
		adminnewsservice.uptdateNews(n);
		return "ok";
	}
	//根据id删除
	@RequestMapping(value="/adminNews/delNews.html")
	@ResponseBody
	public String deleteNews(Integer id) {
		adminnewsservice.deleteNewsById(id);
		return "ok";
	}
	
	//删除多选
	@RequestMapping(value="/adminNews/deleteNewss.html")
	@ResponseBody
	public String deleteNewsss(Integer[] ids) {
		//执行删除操作
		adminnewsservice.deleteNewsByIds(ids);
		return "ok";
	}
			

}
